/*
 * WorkPage.cpp
 *
 *  Created on: 2010-12-9
 *      Author: wenjian
 */

#include "WorkPage.h"
#include "BackgroundLayer.h"
#include "ToolsLayer.h"
#include "ControlLayer.h"
#include "WorkLayer.h"
#include "StatusLayer.h"
#include "OperationLayer.h"

namespace S {

  WorkPage::WorkPage(HWND hWnd):DrawPage(hWnd, 10, 10, 1000, 600), workMode(SelectMode){
    backgroundLayer = new BackgroundLayer(this);
    toolsLayer = new ToolsLayer(this);
    workLayer = new WorkLayer(this);
    workLayer->setFocus();
    controlLayer = new ControlLayer(this, workLayer);
    statusLayer = new StatusLayer(this);
    operationLayer = new OperationLayer(this);

    addLayer(backgroundLayer);
    addLayer(workLayer);
    addLayer(controlLayer);
    addLayer(toolsLayer);
    addLayer(statusLayer);
    addLayer(operationLayer);

    workLayer->setGeometry(80, 50, width()-310, height()-100);
    backgroundLayer->setGeometry(-1, -1, width()-1, height()+1);
    toolsLayer->setGeometry(10, 10, width()-240, 30);
    controlLayer->setGeometry(10, 50, 60, height()-100);
    statusLayer->setGeometry(10, height()-40, width()-240, 30);
    operationLayer->setGeometry(width()-220, 10, 210, height()-60);

    workLayer->setBackground(Gdiplus::Color(220,245,245,245));
    toolsLayer->setBackground(Gdiplus::Color(200,120,120,120));
    controlLayer->setBackground(Gdiplus::Color(200,120,120,120));
    statusLayer->setBackground(Gdiplus::Color(255,0,0,0));
    operationLayer->setBackground(Gdiplus::Color(200,120,120,120));

    bitbltLayerMerger = new BitbltLayerMerger(this);
    transparentBitbltLayerMerger = new TransparentBitbltLayerMerger(this, RGB(0,0,0));

    //setMouseEventMode(MEventMode_Top);
    //setMouseEventMode(MEventMode_Manual, toolsLayer);

    refresh(true);
  }

  WorkPage::~WorkPage() {
    delete backgroundLayer;
    delete toolsLayer;
    delete controlLayer;
    delete workLayer;
    delete statusLayer;
    delete operationLayer;
    //if(layerMerger)delete layerMerger;

    delete bitbltLayerMerger;
    delete transparentBitbltLayerMerger;
  }


  /**
   * 设置工作模式
   */
  void WorkPage::setWorkMode(WorkMode mode){
    workMode = mode;
    workLayer->switchCursor();
  }



  /**
   * 当鼠标在画布上按键时
   */
  void WorkPage::mousePressEvent(const MouseEvent* e){
//    cout<<"WorkPage.mousePressEvent.e.x() is "<<e->x()<<endl;
//    cout<<"WorkPage.mousePressEvent.e.y() is "<<e->y()<<endl;
    Layer* layer = (Layer*)getMouseHoveringLayer();
//    cout<<"WorkPage::mousePressEvent.getMouseHoveringLayer() is "<<layer<<endl;
    if(layer && (layer==toolsLayer || layer==statusLayer || layer==controlLayer)){
      addLayer(layer, getLayers()->size()); //调整位置
//      movingLayer = layer;
      layer->prepareTransform(e->x(), e->y()); //准备移动

    }
  }

  /**
   * 当鼠标在画布上移动时
   */
  void WorkPage::mouseMoveEvent(const MouseEvent* e){
    //cout<<"WorkPage.mouseMoveEvent.e.x() is "<<e->x()<<endl;
    //cout<<"WorkPage.mouseMoveEvent.e.y() is "<<e->y()<<endl;
    //移动层
    Layer* layer = (Layer*)getMouseHoveringLayer();
    //cout<<"WorkPage.getMouseHoveringLayer layer "<<layer<<endl;
    if(layer && ((layer==toolsLayer && toolsLayer->isControlAnchor((Element*)layer->getMouseHoveringElement()))
                 || layer==statusLayer || layer==controlLayer)
          && e->pressed(MButton_Left)
          && layer->isPreparingTransform()
        ){
      layer->moveTo(e->x(), e->y());
    }
  }

  void WorkPage::mouseReleaseEvent(const MouseEvent* e){
    Layer* layer = (Layer*)getMouseHoveringLayer();
    if(layer && (layer==toolsLayer || layer==statusLayer || layer==controlLayer)){
      layer->endTransform();//结束移动层
    }
  }


}
